import 'package:flutter/cupertino.dart';

class SlideTransitionX extends AnimatedWidget{

  Animation<double> get position => listenable;
  final Widget child; // widget
  final AxisDirection direction; // 定义方向
  Tween<Offset> _tween;

  SlideTransitionX({
    Key key, 
    @required Animation<double> position,
    this.direction = AxisDirection.down,
    this.child,
  }):assert(position != null),
    super(key:key, listenable: position){
      /// 偏移量值初始化在内部处理
      switch(direction){
        case AxisDirection.up:
        _tween = Tween(begin: Offset(0, 1), end: Offset(0, 0));
        break;
      case AxisDirection.right:
        _tween = Tween(begin: Offset(-1, 0), end: Offset(0, 0));
        break;
      case AxisDirection.down:
        _tween = Tween(begin: Offset(0, -1), end: Offset(0, 0));
        break;
      case AxisDirection.left:
        _tween = Tween(begin: Offset(1, 0), end: Offset(0, 0));
        break;
      }
    }

  @override
  Widget build(BuildContext context) {
    
    /// position Animaiton<double> 对象
    Offset offset = _tween.evaluate(position);
    /// AnimationStatus 是一个枚举类
    if(position.status == AnimationStatus.reverse){
      switch(direction){
        case AxisDirection.up:
          offset = Offset(offset.dx, -offset.dy);
          break;
        case AxisDirection.right:
          offset = Offset(-offset.dx, offset.dy);
          break;
        case AxisDirection.down:
          offset = Offset(offset.dx, -offset.dy);
          break;
        case AxisDirection.left:
          offset = Offset(-offset.dx, offset.dy);
          break;
      }
    }

    return FractionalTranslation(
      translation: offset,
      child: child,
    );
  }
  
}
